package com.coocaa.ops.admin.v1.dmp.biz.impl;

import com.coocaa.ops.admin.v1.dmp.biz.IDmpRegistryRlsInfoBiz;
import com.coocaa.ops.admin.v1.dmp.mapper.IDmpRegistryRlsInfoMapper;
import com.coocaa.ops.admin.v1.dmp.util.DmpRegistryRlsUtil;
import com.coocaa.ops.common.core.base.BaseBizImpl;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.biz.dmp.entity.DmpRegistryRlsInfoEntity;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bjh
 * @description dmp定向关联信息-biz层接口实现
 * @date 2020-06-13 11:09:36
 */
@Service
public class DmpRegistryRlsInfoBizImpl extends BaseBizImpl<IDmpRegistryRlsInfoMapper, DmpRegistryRlsInfoEntity> implements IDmpRegistryRlsInfoBiz, InitializingBean {
    /**
     * 注入到工具类
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        DmpRegistryRlsUtil.setDmpRegistryRlsInfoBiz(this);
    }

    @Override
    public boolean isResourceHasUsed(Integer id, String type) {
        QueryTemplate<DmpRegistryRlsInfoEntity> queryTemplate = QueryTemplate.getEqQueryTemplate("flag", 1);
        queryTemplate.lambda().eq(DmpRegistryRlsInfoEntity::getSubContainerId, id)
                .eq(DmpRegistryRlsInfoEntity::getSubContainerType, type);
        return getMapper().selectCount(queryTemplate) > 0;
    }

    /**
     * 删除关联信息-通过父容器的ID-类型-版本
     *
     * @param id
     * @param type
     * @param version
     */
    @Override
    public void deleteDmpRls(Integer id, String type, Integer version) {
        QueryTemplate<DmpRegistryRlsInfoEntity> queryTemplate = QueryTemplate.getEqQueryTemplate("flag", 1);
        queryTemplate.lambda().eq(DmpRegistryRlsInfoEntity::getParentContainerId, id)
                .eq(DmpRegistryRlsInfoEntity::getParentContainerType, type)
                .eq(DmpRegistryRlsInfoEntity::getParentContainerVersion, version);
        if (getMapper().selectCount(queryTemplate) > 0) {
            getMapper().delete(queryTemplate);
        }
    }

    /**
     * 删除关联信息-通过父容器的ID列表-类型-版本
     *
     * @param id
     * @param type
     * @param version
     */
    @Override
    public void deleteDmpRls(List<Integer> id, String type, Integer version) {
        QueryTemplate<DmpRegistryRlsInfoEntity> queryTemplate = QueryTemplate.getEqQueryTemplate("flag", 1);
        queryTemplate.lambda().in(DmpRegistryRlsInfoEntity::getParentContainerId, id)
                .eq(DmpRegistryRlsInfoEntity::getParentContainerType, type)
                .eq(DmpRegistryRlsInfoEntity::getParentContainerVersion, version);
        if (getMapper().selectCount(queryTemplate) > 0) {
            getMapper().delete(queryTemplate);
        }
    }

    /**
     * 根据子容器id类型删除关联信息
     * @param subId
     * @param type
     */
    public void deleteDmpBySubId(Integer subId, String type){
        QueryTemplate<DmpRegistryRlsInfoEntity> queryTemplate = QueryTemplate.getEqQueryTemplate("flag", 1);
        queryTemplate.lambda().in(DmpRegistryRlsInfoEntity::getSubContainerId, subId)
                .eq(DmpRegistryRlsInfoEntity::getSubContainerType, type);
        if (getMapper().selectCount(queryTemplate) > 0) {
            getMapper().delete(queryTemplate);
        }

    }
}